package com.woniu113.junk.site.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.site.client.UserClient;
import com.woniu113.junk.site.client.dto.AdminInfo;
import com.woniu113.junk.site.client.dto.AdminRole;
import com.woniu113.junk.site.dto.AllWasteTransferStatisticsDTO;
import com.woniu113.junk.site.dto.WasteTransferStatisticsDTO;
import com.woniu113.junk.site.exception.WasteTransferException;
import com.woniu113.junk.site.mapper.InventoryMapper;
import com.woniu113.junk.site.mapper.StationInfoMapper;
import com.woniu113.junk.site.mapper.WasteTypeMapper;
import com.woniu113.junk.site.model.*;
import com.woniu113.junk.site.mapper.WasteTransferStatisticsMapper;
import com.woniu113.junk.site.param.wastetransfer.AddWasteTransferParam;
import com.woniu113.junk.site.param.wastetransfer.FindWasteTransferParam;
import com.woniu113.junk.site.service.WasteTransferStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.service.WasteTypeService;
import com.woniu113.junk.site.util.Data;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class WasteTransferStatisticsServiceImpl extends ServiceImpl<WasteTransferStatisticsMapper, WasteTransferStatistics> implements WasteTransferStatisticsService {

    @Resource
    private WasteTransferStatisticsMapper wasteTransferStatisticsMapper;
    @Resource
    private StationInfoMapper stationInfoMapper;
    @Resource
    private UserClient userClient;
    @Resource
    private WasteTypeMapper wasteTypeMapper;
    @Resource
    private InventoryMapper inventoryMapper;


    @Override
    public WasteTransferStatistics addWasteTransfer(AddWasteTransferParam param, Integer adminId) {
        //1.验证数据完整性
        if (param.getStationId()==null||param.getCountyStationId()==null||param.getDoId()==null||param.getWasteList().isEmpty()||param.getWasteList().size()==0){
            throw new WasteTransferException("请将数据填写完整",9001);
        }
        //2.验证区站点是否存在
        QueryWrapper<StationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",param.getCountyStationId()).eq("station_state", Data.STATION_STATE_OPEN);
        StationInfo stationInfo = stationInfoMapper.selectOne(queryWrapper);
        if (stationInfo==null){
            throw new WasteTransferException("区站点不存在",9002);
        }
        //3.验证子站点是否存在
        QueryWrapper<StationInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("station_id",param.getStationId()).eq("station_state", Data.STATION_STATE_OPEN);
        StationInfo stationInfo1 = stationInfoMapper.selectOne(queryWrapper1);
        System.out.println("+++"+stationInfo1.toString());
        if (stationInfo1==null){
            throw new WasteTransferException("子站点不存在",9003);
        }
        //4.验证骑手是否存在
        AdminInfo adminInfo = userClient.selectAdminById(param.getDoId()).getData();
        AdminRole adminRole = userClient.selectRoleById(adminInfo.getRoleId()).getData();
        if (!adminRole.getRoleType().equals("骑手")){
            throw new WasteTransferException("该骑手不存在",9004);
        }
        //5.验证废品是否存在以及废品重量是否合理
        for (AddWasteTransferParam.WasteInfo wasteInfo : param.getWasteList()) {
            QueryWrapper<WasteType> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("type_id",wasteInfo.getTypeId()).eq("type_state",Data.WASTE_TYPE_STATE_NORMAL);
            WasteType wasteType = wasteTypeMapper.selectOne(queryWrapper2);
            if (wasteType==null){
                throw new WasteTransferException("有废品信息不存在",9005);
            }
            if (wasteInfo.getWeight()<=0){
                throw new WasteTransferException("有废品重量不合理",9006);
            }
        }
        //6.创建废品转移信息
        WasteTransferStatistics wasteTransferStatistics = new WasteTransferStatistics();
        wasteTransferStatistics.setCountyStationId(param.getCountyStationId());//区站点id
        wasteTransferStatistics.setStationId(param.getStationId());//子站点id
        wasteTransferStatistics.setCreateTime(LocalDateTime.now());//创建时间
        wasteTransferStatistics.setWasteInfo(JSONUtil.parse(param.getWasteList()).toString());//废品详情
        wasteTransferStatistics.setState(Data.TRANSFER_WAIT);//状态
        wasteTransferStatistics.setAdminId(adminId);//创建人
        wasteTransferStatistics.setDoId(param.getDoId());//骑手
        wasteTransferStatisticsMapper.insert(wasteTransferStatistics);
        //7.减少原子站点库存量
        for (AddWasteTransferParam.WasteInfo wasteInfo : param.getWasteList()) {
            QueryWrapper<Inventory> queryWrapper2 = new QueryWrapper<>();
            System.out.println("***"+stationInfo1.getStationId());
            System.out.println("55"+wasteInfo.getTypeId());
            queryWrapper2.eq("station_id",stationInfo1.getStationId()).eq("type_id",wasteInfo.getTypeId());
            Inventory inventory = inventoryMapper.selectOne(queryWrapper2);
            System.out.println("----"+inventory);
            System.out.println("1"+inventory.getNumber());
            System.out.println("2"+wasteInfo.getWeight());
            inventory.setNumber(inventory.getNumber()-wasteInfo.getWeight());
            inventoryMapper.updateById(inventory);
        }
        return wasteTransferStatistics;
    }

    @Override
    public void transferStart(Integer transferId, Integer adminId) {
        //1.判断数据完整性
        if (transferId==null||adminId==null){
            throw new WasteTransferException("请将数据填写完整",9001);
        }
        //2.判断转移信息是否存在
        QueryWrapper<WasteTransferStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transfer_id",transferId).eq("state",Data.TRANSFER_WAIT);
        WasteTransferStatistics wasteTransferStatistics = wasteTransferStatisticsMapper.selectOne(queryWrapper);
        if (wasteTransferStatistics==null){
            throw new WasteTransferException("不存在该转移信息",9007);
        }
        //3.判断当前登录用于废品转移表中的doId是相同的
        if (wasteTransferStatistics.getDoId()!=adminId){
            throw new WasteTransferException("不是该骑手的转移信息,不能转移",9008);
        }
        //4.修改转移表中的信息(开始时间/状态)
        wasteTransferStatistics.setStartTime(LocalDateTime.now());
        wasteTransferStatistics.setState(Data.TRANSFER_ING);
        wasteTransferStatisticsMapper.updateById(wasteTransferStatistics);
    }

    @Override
    public AllWasteTransferStatisticsDTO findWasteTransfer(FindWasteTransferParam param, Integer offset) {

        QueryWrapper<WasteTransferStatistics> queryWrapper = new QueryWrapper<>();
        if (param.getState() != null){
            queryWrapper.eq("state",param.getState());
        }
        if (param.getStationId() != null){
            queryWrapper.eq("station_id",param.getStationId());
        }
        if (param.getCountyStationId() != null){
            queryWrapper.eq("county_station_id",param.getCountyStationId());
        }
        int size = Math.toIntExact(wasteTransferStatisticsMapper.selectCount(queryWrapper));
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<WasteTransferStatistics> infos = wasteTransferStatisticsMapper.selectList(queryWrapper);

        ArrayList<WasteTransferStatisticsDTO> wasteTransferStatisticsDTOS = new ArrayList<>();
        for (WasteTransferStatistics info : infos) {
            WasteTransferStatisticsDTO wasteTransferStatisticsDTO = BeanUtil.toBean(info, WasteTransferStatisticsDTO.class);
            AdminInfo adminInfo = userClient.selectAdminById(info.getAdminId()).getData();
            wasteTransferStatisticsDTO.setAdminName(adminInfo.getAdminName());
            AdminInfo adminInfo1 = userClient.selectAdminById(info.getDoId()).getData();
            wasteTransferStatisticsDTO.setDoName(adminInfo1.getAdminName());
            wasteTransferStatisticsDTO.setDoPhone(adminInfo1.getAdminPhone());
            StationInfo stationInfo = stationInfoMapper.selectById(info.getStationId());
            wasteTransferStatisticsDTO.setStationName(stationInfo.getStationName());
            StationInfo stationInfo1 = stationInfoMapper.selectById(info.getCountyStationId());
            wasteTransferStatisticsDTO.setCountyStationName(stationInfo1.getStationName());
            wasteTransferStatisticsDTOS.add(wasteTransferStatisticsDTO);
        }
        AllWasteTransferStatisticsDTO dto = new AllWasteTransferStatisticsDTO();
        dto.setList(wasteTransferStatisticsDTOS);
        dto.setTotal(size);
        return dto;
    }
}
